package com.somsoft.unlockme;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.MotionEvent;

import com.somsoft.unlockme.db.Level;
import com.somsoft.unlockme.db.Service;
import com.somsoft.unlockme.fk.C;

public class UnlockMeActivity extends Activity {

	public int screenWidth;
	public int screenHeight;
	private DisplayMetrics dm;
	private UnlockMeView jv;
	
	int touchX = 0;
	int touchY = 0;
	int xx = 0;
	int yy = 0;
	//主线程的Handler对象
	Handler myHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {//重写处理消息方法
			if(msg.what==0){
				success();
			}else if(msg.what==1){
				//select
				goMain();
			}else if(msg.what==2){
				//reload
				reloadMap();
			}else if(msg.what==3){
				//next加载下一关
				loadNext();
			}else if(msg.what==4){
				//jump
				jump();
			}
		}		
	};
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        MyApplication.getInstance().addActivity(this);
        dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		screenWidth = dm.widthPixels;
		screenHeight = dm.heightPixels;
		jv = new UnlockMeView(this);
        setContentView(jv);
        
    }
	
	public void getNextInfo(){
		C.moveable = true;
		Service service = new Service(this);  
		C.nextLevel =  service.find(C.currLevel+1);
	}
	
	private void goMain(){
		C.moveable = true;
		jv.thread.flag = false;
		finish();
		Intent it = new Intent(this,MainActivity.class);
		startActivity(it);
	}
	
	private void loadNext(){
		//加载下一关
		UnlockMeView.fks.clear();
		C.currLevel++;
		UnlockMeView.fks = Maps.get(C.currLevel);
		C.moveable = true;
		C.currMoves = 0;
		Service service = new Service(this);  
		C.score =  service.find(C.currLevel).getScore();
		C.icons.clear();
		C.nextLevel = service.find(C.currLevel+1);
	}
	
	private void reloadMap(){
		UnlockMeView.fks.clear();
		UnlockMeView.fks = Maps.get(C.currLevel);
		C.moveable = true;
		C.currMoves = 0;
	}
	
	private void jump(){
		//跳关
		/*
		 * 1、检查积分
		 * 2、扣除积分
		 */
		Service service = new Service(this);
		if(C.currLevel<C.MAX_LEVEL){
			//减分，解锁下一关
			Level lv = service.find(C.currLevel +1);
			if("Y".equals(lv.getIsLock())){
				lv.setIsLock("N");
				service.update(lv);
			}
			C.currLevel++;
			UnlockMeView.fks = Maps.get(C.currLevel);
			C.isGetLevel = true;
			
			Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("跳关成功！");  
			builder.setTitle("提示");  
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
				}
			});
			builder.create().show();
			C.icons.clear();
		}
	}
	
	private void success(){
		//解锁下一关
		Service service = new Service(this);  
		if(C.currLevel<C.MAX_LEVEL){
			C.nextLevel = service.find(C.currLevel +1);
			if("Y".equals(C.nextLevel.getIsLock())){
				C.nextLevel.setIsLock("N");
				service.update(C.nextLevel);
			}
		}else{
			//通关
		}
		//更新积分
		Level lv = service.find(C.currLevel);
		if(lv.getBest()==0 || C.currMoves<lv.getBest()){
			lv.setBest(C.currMoves);
		}
		if(C.currMoves<=15){
			lv.setScore(3);
			C.score = 3;
		}else if(C.currMoves<=40){
			lv.setScore(2);
			C.score = 2;
		}else{
			lv.setScore(1);
			C.score = 1;
		}
		service.update(lv);
		C.lvs = service.getLevels((C.currPage - 1) * 9, 9);
		C.icons.clear();
	}
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_HOME){
			jv.thread.flag = false;
			Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("确认退出游戏吗？");  
			builder.setTitle("提示");  
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
					Intent it = new Intent(UnlockMeActivity.this,MainActivity.class);
					startActivity(it);
				}
			});
			builder.setNegativeButton("取消",new DialogInterface.OnClickListener(){
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			builder.create().show();
			return true;
		}
		return super.onKeyUp(keyCode, event);
	}
	
	
	public boolean onTouchEvent(MotionEvent event) {
		int iAction = event.getAction();
		if (iAction == MotionEvent.ACTION_CANCEL
				|| iAction == MotionEvent.ACTION_DOWN
				|| iAction == MotionEvent.ACTION_MOVE) {
			return false;
		}
		touchX = (int) event.getX();
		touchY = (int) event.getY();
		return super.onTouchEvent(event);
	}
	

}














